/*
 * @Author: SingleBiu 854390236@qq.com
 * @Date: 2024-12-27 12:21:23
 * @LastEditors: SingleBiu 854390236@qq.com
 * @LastEditTime: 2024-12-27 13:10:44
 * @FilePath: /photograph/src/main.c
 * @Description: 
 * 
 */
#include"lcd.h"
#include"bmp.h"
#include"jpg.h"
#include"ts.h"
#include"node.h"
#include<pthread.h>

int ts_fd;
// 按钮
int btn_l = FALSE;
int btn_r = FALSE;
// 触摸屏输入的值
int TOUCH_EVENT = MOVE_EMPTY;
Head_Node *H = NULL;

/* 356x24  */
char note1[] = {
0x00,0x06,0x00,0x00,0x00,0x00,0x02,0x03,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,
0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x10,0x0C,0x00,0x03,0x03,
0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,
0x00,0x06,0x00,0x38,0x7D,0xF8,0x06,0x03,0x00,0x07,0xFF,0xF8,0x00,0x20,0x00,0x0F,
0xFF,0xF0,0x02,0x1F,0xFC,0x0F,0xE3,0xF8,0x3F,0x84,0x00,0x0C,0x61,0x88,0x06,0x03,
0x00,0x04,0x0C,0x08,0x00,0x30,0x00,0x00,0x00,0x60,0x02,0x18,0x0C,0x0C,0x62,0x10,
0x01,0x8C,0x00,0x06,0x61,0x88,0x04,0xFF,0xFE,0x04,0x0C,0x08,0x1F,0xFF,0xE0,0x00,
0x01,0xC0,0x02,0x18,0x0C,0x0C,0x62,0x10,0x01,0x8C,0x00,0x00,0x61,0x88,0x0C,0x03,
0x00,0x04,0x0C,0x08,0x10,0x20,0x60,0x00,0x03,0x00,0x02,0x18,0x0C,0x0C,0x62,0x10,
0x01,0x8F,0xFC,0x00,0x61,0x88,0x0C,0x03,0x00,0x04,0x0C,0x08,0x10,0x20,0x60,0x00,
0x06,0x00,0x3F,0xF8,0x0C,0x0C,0x62,0x10,0x31,0x98,0x0C,0x00,0x61,0x88,0x1C,0x7F,
0xF8,0x04,0x0C,0x08,0x10,0x20,0x60,0x00,0x1C,0x00,0x06,0x1F,0xFC,0x0C,0x62,0x10,
0x19,0x18,0x08,0x00,0x61,0x88,0x1C,0x63,0x08,0x07,0xFF,0xF8,0x10,0x20,0x60,0x00,
0x18,0x00,0x06,0x18,0x0C,0x0C,0x62,0x10,0x0D,0x33,0x98,0x7C,0x61,0x88,0x34,0x63,
0x08,0x04,0x0C,0x08,0x18,0x30,0x60,0x00,0x18,0x00,0x0F,0x18,0x0C,0x0C,0x62,0x18,
0x07,0x63,0x00,0x0C,0x61,0x88,0x64,0x63,0x08,0x04,0x0C,0x08,0x1F,0xFF,0xE0,0x7F,
0xFF,0xFE,0x0F,0x98,0x0C,0x7F,0xFF,0xFE,0x06,0x03,0x00,0x0C,0x61,0x88,0x64,0x63,
0x08,0x04,0x0C,0x08,0x10,0x20,0x60,0x00,0x18,0x00,0x1A,0xD8,0x0C,0x0C,0x62,0x10,
0x07,0x03,0x00,0x0C,0x61,0x88,0x04,0x7F,0xF8,0x04,0x0C,0x08,0x10,0x20,0x60,0x00,
0x18,0x00,0x1A,0x58,0x0C,0x0C,0x62,0x10,0x07,0x83,0x00,0x0C,0x6D,0x88,0x04,0x03,
0x00,0x07,0xFF,0xF8,0x10,0x20,0x60,0x00,0x18,0x00,0x32,0x1F,0xFC,0x0C,0x62,0x10,
0x0D,0x83,0x80,0x0C,0x79,0x88,0x04,0x13,0x00,0x04,0x0C,0x08,0x18,0x30,0x60,0x00,
0x18,0x00,0x62,0x18,0x0C,0x0C,0x62,0x10,0x18,0xC6,0x80,0x0C,0x71,0xB8,0x04,0x1B,
0x00,0x04,0x0C,0x08,0x1F,0xFF,0xE0,0x00,0x18,0x00,0x02,0x18,0x0C,0x0C,0x66,0x10,
0x18,0xC6,0xC0,0x0C,0x01,0xB8,0x04,0x0E,0x00,0x0C,0x0C,0x08,0x00,0x20,0x00,0x00,
0x18,0x00,0x02,0x18,0x0C,0x08,0x66,0x10,0x30,0x0C,0x40,0x0C,0x01,0x80,0x04,0x0E,
0x00,0x0C,0x0C,0x08,0x00,0x20,0x00,0x00,0x18,0x00,0x02,0x18,0x0C,0x08,0x66,0x10,
0x20,0x08,0x60,0x0E,0x01,0x80,0x04,0x0F,0x00,0x0C,0x0C,0x08,0x00,0x20,0x04,0x00,
0x18,0x00,0x02,0x18,0x0C,0x18,0x64,0x10,0x00,0x38,0x30,0x3F,0x80,0x00,0x04,0x1F,
0xC0,0x18,0x0C,0x08,0x00,0x20,0x0C,0x00,0x18,0x00,0x02,0x1F,0xFC,0x10,0x6C,0x10,
0x00,0x70,0x1C,0x30,0xF8,0x7C,0x04,0x78,0xF0,0x18,0x00,0x78,0x00,0x30,0x1C,0x00,
0xF8,0x00,0x02,0x18,0x0C,0x31,0xFC,0xF0,0x01,0xE0,0x0E,0x00,0x3F,0xFC,0x05,0xE0,
0x3E,0x30,0x00,0x70,0x00,0x1F,0xF8,0x00,0xF0,0x00,0x02,0x18,0x0C,0x21,0xC8,0xE0,
0x00,0x80,0x04,0x00,0x00,0x00,0x04,0xC0,0x04,0x30,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

void *thread_touch_click()
{
    while (1)
    {
        get_user_input_click(ts_fd);
    }
    
}

int main(int argc, char *argv[])
{
    // LCD初始化
    lcd_init();
    //清屏
    lcd_draw_rect(0,0,WIDTH,HEIGHT,WHITE);

    //初始化图片链表
    H = init_Node();

    //读取目录
    DIR *dir = opendir(argv[1]);
    readdirent(dir,argv[1]);

    //定义一个指针指向图片链表
    Node *ptr = H->pic_list;

    //显示开始的图片
    bmp_display("startup.bmp",0,0);
    
    //显示两个按钮
    jpg_displasy("left.jpg",0,600);
    jpg_displasy("right.jpg",560,600);

    lcd_draw_word(note1,sizeof(note1),192,BLACK,0,500);

    //初始化触摸屏
    ts_fd = touch_init();

    //开启一个线程检测触摸
    pthread_t ts;
    int res = pthread_create(&ts,NULL,thread_touch_click,NULL);
    if (res != 0)
    {
        perror("Failed to create pthread");
        exit(res);
    }

    //上一张 下一张图片具体实现
    while (1)
    {
        if (TRUE == btn_l)
        {
            lcd_draw_rect(0,0,WIDTH,HEIGHT,WHITE);
            // 上一张
            ptr = ptr->prev;
            display(ptr->pathname,0,0);
            btn_l = FALSE;
            jpg_displasy("left.jpg",0,600);
            jpg_displasy("right.jpg",560,600);
        }
        
        if (TRUE == btn_r)
        {
            lcd_draw_rect(0,0,WIDTH,HEIGHT,WHITE);
            // 下一张
            ptr = ptr->next;
            display(ptr->pathname,0,0);
            btn_r = FALSE;
            jpg_displasy("left.jpg",0,600);
            jpg_displasy("right.jpg",560,600);
        }   
    }

    while (1)
    {
        /* code */
    }
    
    //关闭LCD屏幕
    close(lcd_fd);

    return 0;
}